<?php

namespace app\controllers;

use Yii;
use app\base\Controller;
use app\models\Menu;
use app\models\Admin;
use yii\helpers\Url;

class SiteController extends Controller
{
    public $enableCsrfValidation = false;
    public function actions()
    {
        return [
            'error' => [
                'class' => 'yii\web\ErrorAction',
            ],
            'captcha' => [
                'class' => 'yii\captcha\CaptchaAction',
                'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
            ],
        ];
    }

    // 系统首页
    public function actionIndex()
    {        
        $params         = Yii::$app->params;
        
      
        $session        = Yii::$app->session;
       // echo '<pre>';
       // print_R($session);
        
        $act_web    = $session['admin_info']['action_web'];
        $act_web_info = $params['sites'][$act_web];
        $admin_info     = $session['admin_info'];
       // print_R($admin_info);
        
        $web_list       = array();
        if ($admin_info['powers'] == 'all') {
            $web_list   = $params['sites'];
          
        } else {
            foreach ($params['sites'] as $k=>$v) {
                if (strpos($admin_info['allow_web'],$v['module_id'].'-') !== false || $k == 'sys_set') {
                    $web_list[$k] = $v;
                }
            }
        }
        
        // 默认模块
        $default_route = $act_web_info['default_route'];
        //var_dump($web_list);
       // exit;
        $now_web_Info   = array(
            'name'      => $params['sites'][$admin_info['action_web']]['name'],
            'web_url'   => $params['sites'][$admin_info['action_web']]['web_url'],
            'module_name' => $params['sites'][$admin_info['action_web']]['module_name'],
            'web_list'  => $web_list
        );
        
        $menu = Menu::get_list();
        // echo '<pre>';
        // print_R($menu);
        // exit;
        $loginout_url = Url::toRoute(['site/loginout']);
        return $this->renderPartial('index', [
            'admin_real_name'       => $admin_info['real_name'],
            'menu'                  => $menu,
            'loginout_url'          => $loginout_url,
            'now_web_info'          => $now_web_Info,
            'default_route' => $default_route
        ]);
    }

    // 登陆
    public function actionLogin()
    {
        $info = Admin::admin_login();
        if ($info['state']==1) {
            yii::$app->response->redirect($info['url']);
        } else {
            exit($info['msg']);
        }
    }
    
    public function actionSynadmin()
    {
        $info = Admin::syn_admin();
        echo json_encode($info);
        exit;
    }

    // 登出
    public function actionLoginout()
    {
        $session = Yii::$app->session;
        $session->remove('admin_info');
        
        $returnUrl 		= "http://".$_SERVER['HTTP_HOST'];
	$strurli 		= base64_encode($returnUrl);	
	$red_url		= Yii::$app->params['sso_server_url'].'/login/index/loginout/?returnurl='.$strurli;
	header ('Location: '.$red_url);
        exit;
    }

    // 导航站点切换
    public function actionSwitchweb()
    {
        $sweb           = Yii::$app->request->get('sweb');
        $web_list       = Yii::$app->params['sites'];
        $w_url          = Url::toRoute(['site/index']);
        $rtn_arr        = ['state'=>'1','msg'=>'OK','sweb'=>$sweb,'href'=>$w_url];
        if ($sweb == '') {
            $rtn_arr['state'] = 0;
            $rtn_arr['msg'] = '数据为空!';
            echo json_encode($rtn_arr);
            exit;
        }
        $session = Yii::$app->session;
        
        // 根据sweb值获取模块id值
        $sweb_module_id = Yii::$app->params['sites'][$sweb]['module_id'];
        // echo '<pre>';
        // print_R($session['admin_info']['allow_web']);
        // exit;
        if ($sweb == 'sys_set' || strpos($session['admin_info']['allow_web'],$sweb_module_id.'-') !== false || ($session['admin_info']['powers'] == 'all' && isset($web_list[$sweb])) ) {
            $tmp_admin = [];
            foreach ($session['admin_info'] as $k=>$v) {
                $tmp_admin[$k]  = $v;
            }
            $tmp_admin['action_web'] = $sweb;
            $session['admin_info'] = $tmp_admin;
        } else {
            $rtn_arr['state'] = 0;
            $rtn_arr['msg'] = '未找到对应的站点!';
            echo json_encode($rtn_arr);
            exit;
        }
        
        echo json_encode($rtn_arr);
        exit;
    }

    // 系统默认首页
    public function actionAbout()
    {
        $site = Yii::$app->request->get('site_name');
        if (empty($site)) {
            $site =  Yii::$app->request->get('site');
            $data['name'] = Yii::$app->params['sites'][$site]['name'];
        } else {
            $data['name'] = $site;
        }
        return $this->render('about', ['data' => $data]);
    }

    public function actionDiff()
    {
        $data = require(__DIR__ . '/../phpunit/data/diff-test.php');
        $diff = yii::$app->diff->create(array_merge($data, ['diffDetailFields' => 'content']));
        yii::$app->helper->adminlog(rand(1, 9999), $diff);
        return $this->render('diff', [
            'diff' => $diff,
            'detail2' => \app\models\ActionHistory::findOne(24)->detail
        ]);
    }
}
